﻿using System;
using System.Data;
using System.Security.Principal;
using System.Web.Services;
using DevExpress.Web.ASPxTreeView;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;

public partial class TreeViewPage : System.Web.UI.Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {
        //WebService webservice = new WebService();
        //webservice.InitMySql();

        //initialTreeView();
    }

    protected void initialTreeView()
    {
        treeView.Nodes.Clear();

        string sql = "Select year from webTest.Test group by year";

        DataTable dataTable = WebService.dataBaseHelper.GetDataTable(sql);
        foreach (DataRow dataRow in dataTable.Rows)
        {
            string value = dataRow[0].ToString();
            TreeViewNode node = new TreeViewNode(value, value);
            treeView.Nodes.Add(node);
        }

        treeView.ExpandAll();
    }

    protected void InitialChartData(string year)
    {
        webChart.Titles.Clear();
        ChartTitle newTitle = new ChartTitle { Text = year + " 数据" };
        webChart.Titles.Add(newTitle);

        Series series = webChart.Series["Series1"];
        if (series != null)
        {
            if (series.Points.Count > 0)
                series.Points.Clear();

            DataTable dataTable =
               WebService.dataBaseHelper.GetDataTable("Select month,value from webTest.Test where year = " + year);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    Int32 month = Convert.ToInt32(dataRow[0]);
                    Int32 value = Convert.ToInt32(dataRow[1]);

                    var seriesPoint = new SeriesPoint(month, value);

                    series.Points.Add(seriesPoint);
                }
            }
        }
    }
    protected void treeView_NodeClick(object source, TreeViewNodeEventArgs e)
    {
        string year = e.Node.Name;
        
        InitialChartData(year);
    }
}